<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('categories', function (Blueprint $table) {
            $table->id();
            $table->string('name', 100)->comment('分类名称');
            $table->string('slug', 100)->unique()->comment('URL别名');
            $table->text('description')->nullable()->comment('分类描述');
            $table->string('image', 255)->nullable()->comment('分类图片路径');
            $table->string('icon', 100)->nullable()->comment('分类图标');
            $table->unsignedBigInteger('parent_id')->nullable()->comment('父分类ID');
            $table->integer('sort_order')->default(0)->comment('排序权重');
            $table->boolean('is_active')->default(true)->comment('是否启用');
            $table->boolean('is_featured')->default(false)->comment('是否推荐');
            $table->json('meta_data')->nullable()->comment('元数据');
            $table->timestamps();
            
            // 索引
            $table->index(['parent_id', 'is_active']);
            $table->index(['sort_order', 'is_active']);
            $table->index('slug');
            
            // 外键约束
            $table->foreign('parent_id')->references('id')->on('categories')->onDelete('cascade');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('categories');
    }
};
